/**
 * 装修货品表管理
 */
var ZxHuopin = {

    zxHuopinFenleiId: "",
    zxHpFlName: "",
    tableId: "zxHuopinTable",
    btyZxKemuId:"",
    btyZxKemuIdView:"",
    condition:{
        btyZxKemuIds:""
    },
    zxHuopinFenleiId:""
};

layui.use(['table', 'admin', 'ax','ztree','func','tree','form','layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var func = layui.func;
    var tree = layui.tree;
    var form = layui.form;
    var layer = layui.layer;



    /**
     * 初始化表格的列
     */
    ZxHuopin.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'btyZxHuopinId', hide: true, title: '主键'},
            //{field: 'ghsView', title: '供货商'},
            {field: 'ghsNames', title: '供货商',width:70},
            {field: 'ghsView', title: '收款人',width:70},
            {field: 'name', sort: true, title: '名称'},
            // {field: 'code', sort: true, title: '编码'},
            {field: 'zhujima', sort: true, title: '助记码'},
            {field: 'guige', sort: true, title: '规格',width:70},
            {field: 'danwei', sort: true, title: '单位',width:70},
            {field: 'danjia', sort: true, title: '单价',width:70},
            // {field: 'btyZxKemuId', sort: true, title: '所属科目'},
            {field: 'btyZxKemuIdView', sort: true, title: '所属科目名称'},
            {field: 'hpflName', sort: true, title: '货品分类名称'},
            {field:'isJgc',sort: true,title: "甲供材",width: 90, align: "center",templet:function(data){
                    switch (data.isJgc) {
                        case "1":return "是";break;
                        case "0":return "否";break;
                        default :return "未设置";break;
                    }
                }},
            {field: 'hpType', title: '货品类型',width: 90, templet: function(dd){
                // 1-硬装 2-软装 3-手工
                if (dd.hpType == 1) {
                 return "硬装";
                } else if (dd.hpType == 2) {
                 return "软装";
                } else if (dd.hpType == 3) {
                 return "手工";
                }
            }},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 200}
        ]];
    };

    function init_ghs(){
        $("#ghs").empty();
        $("#ghs").append("<option value=''>请选择</option>");
        var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getZxList");
        var languageResult = languageAjax.start();
        $.each(languageResult.data, function (index, val) {
            $("#ghs").append("<option value='" + val.value + "'>" + val.label+ "</option>");
        });
        form.render('select');
    }
    init_ghs();

    // 所属货品分类
    $('#zxHpFlName').click(function () {
        var formName = encodeURIComponent("parent.ZxHuopin.zxHpFlName");
        var formId = encodeURIComponent("parent.ZxHuopin.zxHuopinFenleiId");
        var treeUrl = encodeURIComponent("/btyZxHuopinFenlei/tree");

        layer.open({
            type: 2,
            title: '父级名称',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#zxHpFlId").val(ZxHuopin.zxHuopinFenleiId);
                $("#zxHpFlName").val(ZxHuopin.zxHpFlName);
            }
        });
    });

    /**
     * 点击查询按钮
     */
    ZxHuopin.search = function () {
        var queryData = {};
        queryData['name'] = $("#condition").val();
        queryData['btyZxKemuIds'] = ZxHuopin.condition.btyZxKemuIds;
        queryData['ghs'] = $("#ghs").val();
        queryData['hasKemu'] = $("#hasKemu").val();
        queryData['hpType'] = $("#hpType").val();
        //queryData['hpflName'] = $("#zxHpFlName").val();
        table.reload(ZxHuopin.tableId, {where: queryData});
    };

    layui.form.on('submit(btnSubmit)', function () {
        ZxHuopin.hpAdd();
    });
    ZxHuopin.hpAdd = function () {
        var ajax = new $ax(Feng.ctxPath + "/zxHuopin/addItem", function (data) {
            if (data.code == 1) {
                Feng.error("添加失败!" + data.message);
                return;
            }
            Feng.success("添加成功！");
            $("#name").val("");
            table.reload(ZxHuopin.tableId);
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set("name",$("#name").val());
        ajax.set("code",$("#code").val());
        ajax.set("ghs",$("#ghs").val());
        ajax.set("guige",$("#guige").val());
        ajax.set("danwei",$("#danwei").val());
        ajax.set("danjia",$("#danjia").val());
        ajax.set("btyZxKemuId",ZxHuopin.btyZxKemuId);
        ajax.set("btyZxKemuIdView",ZxHuopin.btyZxKemuIdView);
        ajax.set("zxHuopinFenleiId",$("#zxHpFlId").val());
        ajax.set("hpType", 3);
        ajax.start();
        return false;
    }

    var codetext = document.getElementById("name");
    codetext.addEventListener("keyup", function(event) {
        event.preventDefault();
        if (event.keyCode === 13) {
           var naval= $("#name").val();
            if (naval.trim()=="") {
                Feng.error("货品名称不能为空");
                return false;
            }
            // layui.form.on('submit(btnSubmit)', function (data) {
                ZxHuopin.hpAdd();
            // });
        }
    });

    /**
     * 导出excel按钮
     */
    ZxHuopin.exportExcel = function () {
        var checkRows = table.checkStatus(ZxHuopin.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     *
     * 改成 弹出 by zwf 20210119
     */
    ZxHuopin.openEditDlg = function (data) {

        var url = Feng.ctxPath + '/zxHuopin/edit?btyZxHuopinId=' + data.btyZxHuopinId;

        parent.layer.open({
            type: 2,
            title: '修改货品',
            area: ['800px', '800px'],
            content: url,
            end: function () {
                ZxHuopin.search();
            },
            cancel:function (index){
                ZxHuopin.search();
            }
        });
    };


    ZxHuopin.gshDlg = function (data) {
        var url = Feng.ctxPath + '/zxHuopin/editGsh?btyZxHuopinId=' + data.btyZxHuopinId;

        parent.layer.open({
            type: 2,
            title: '设置供货商',
            area: ['800px', '500px'],
            content: url,
            end: function () {
                ZxHuopin.search();
            },
            cancel:function (index){
                ZxHuopin.search();
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ZxHuopin.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/zxHuopin/delete", function (data) {
                if(data.code==200){
                    Feng.success("删除成功!");
                    table.reload(ZxHuopin.tableId);
                }else {
                    Feng.error(data.message);
                }
            });
            ajax.set("btyZxHuopinId", data.btyZxHuopinId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ZxHuopin.tableId,
        url: Feng.ctxPath + '/zxHuopin/list',
        page: true,
        height: "full-129",
        cellMinWidth: 100,
        cols: ZxHuopin.initColumn()
    });

    var ZxKemu = { };
    /**
     * 选择科目时
     */
    ZxKemu.onClickNode = function (obj) {
        ZxHuopin.btyZxKemuId=obj.data.id;
        ZxHuopin.btyZxKemuIdView=obj.data.title;
        $("#btyZxKemuIdView").val(obj.data.title);
        if(obj.data.pid==-1){//如果是顶级
            var firsts=obj.data.children;
            var seconds="";
            for(var i=0;i<firsts.length;i++){
                seconds+=ZxKemu.getchilds(firsts[i].children);
            }
            ZxHuopin.condition.btyZxKemuIds=seconds.substr(0,seconds.length-1);
        }else {

            var childs=obj.data.children;
            if(childs.length==0){
                ZxHuopin.condition.btyZxKemuIds = obj.data.id;
            }else{
                var ids=ZxKemu.getchilds(childs);
                ZxHuopin.condition.btyZxKemuIds=ids.substr(0,ids.length-1);
            }
        }

        ZxHuopin.search();
    };
    ZxKemu.getchilds = function getchilds(childs) {
        var ids="";
        for(var i=0;i<childs.length;i++){
            ids+=childs[i].id+",";
        }
        return ids;
    }
    //初始化左侧门店树
    var ajax = new $ax(Feng.ctxPath + "/zxKemu/layuiTree", function (data) {
        tree.render({
            elem: '#kemuTree',
            data: data,
            click: ZxKemu.onClickNode,
            onlyIconControl: true
        });
    }, function (data) {
    });
    ajax.start();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ZxHuopin.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ZxHuopin.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ZxHuopin.exportExcel();
    });


    //批量设置科目
    $('#btnPlSzKemu').click(function () {
        ZxHuopin.setkemu();
    });

    //批量设置甲供材
    $('#btnIsJgc').click(function () {
        ZxHuopin.setJgc();
    });

    //批量设置甲供材
    $('#btnqxJgc').click(function () {
        ZxHuopin.qxJgc();
    });

    ZxHuopin.setkemu = function () {
        var checkRows = table.checkStatus(ZxHuopin.tableId);
        var data = checkRows.data;
        var ids = "";
        var idsName = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择货品，才能批量设置科目！");
            return false;
        }else {
            for (var i in data) {
                if(ids==""){
                    ids=data[i].btyZxHuopinId;
                    idsName=data[i].name;
                }else {
                    ids+= ","+data[i].btyZxHuopinId;
                    idsName+= ","+data[i].name;
                }
            }
        }

        //弹出设置
        var url = Feng.ctxPath + "/zxHuopin/setkemu?btyZxHuopinIds="+ids+"&btyZxHuopinIdName="+encodeURI(idsName);
        layer.open({
            type: 2,
            title: '设置科目',
            area: ['800px', '800px'],
            content: url,
            end: function () {
                ZxHuopin.search();
            },
            cancel:function (index){
                ZxHuopin.search();
            }
        });
    };
    ZxHuopin.setJgc = function () {
        var checkRows = table.checkStatus(ZxHuopin.tableId);
        var data = checkRows.data;
        var ids = "";
        var idsName = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择货品，才能批量设置！");
            return false;
        }else {
            for (var i in data) {
                if(ids==""){
                    ids=data[i].btyZxHuopinId;
                    idsName=data[i].name;
                }else {
                    ids+= ","+data[i].btyZxHuopinId;
                    idsName+= ","+data[i].name;
                }
            }
        }

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/zxHuopin/setJgc", function (data) {
                if(data.code==200){
                    Feng.success("设置成功!");
                    table.reload(ZxHuopin.tableId);
                }else {
                    Feng.error(data.message);
                }
            });
            ajax.set("isJgc", "1");
            ajax.set("btyZxHuopinIds", ids);
            ajax.start();
        };
        Feng.confirm("是否设置甲供材?", operation);
    };
    ZxHuopin.qxJgc = function () {
        var checkRows = table.checkStatus(ZxHuopin.tableId);
        var data = checkRows.data;
        var ids = "";
        var idsName = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择货品，才能批量设置！");
            return false;
        }else {
            for (var i in data) {
                if(ids==""){
                    ids=data[i].btyZxHuopinId;
                    idsName=data[i].name;
                }else {
                    ids+= ","+data[i].btyZxHuopinId;
                    idsName+= ","+data[i].name;
                }
            }
        }

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/zxHuopin/setJgc", function (data) {
                if(data.code==200){
                    Feng.success("设置成功!");
                    table.reload(ZxHuopin.tableId);
                }else {
                    Feng.error(data.message);
                }
            });
            ajax.set("isJgc", "0");
            ajax.set("btyZxHuopinIds", ids);
            ajax.start();
        };
        Feng.confirm("是否取消甲供材?", operation);
    };

    // 工具条点击事件
    table.on('tool(' + ZxHuopin.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ZxHuopin.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ZxHuopin.onDeleteItem(data);
        }
        else if (layEvent === 'ghs') {
            ZxHuopin.gshDlg(data);
        }
    });
});

$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});
